//
//  ESTWorkbenchCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/9/28.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTWorkbenchCell.h"
#import "ESTVerticalValueView.h"
#import "ESTSmallTitleView.h"
#import "TYZCustomSelectedView.h"

@interface ESTWorkbenchCell ()
/**
 *  Cell类型
 */
@property(nonatomic, assign) NSInteger cellType;
@end

@implementation ESTWorkbenchCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    return self;
}

/**
 创建Cell

 @param tableView tableView
 @param section 第几组
 @return 快速创建Cell
 */
+ (instancetype)creatWorkbenchCell:(UITableView *)tableView section:(NSInteger)section {
    ESTWorkbenchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTWorkbenchCell"];
    if (cell == nil) {
        cell = [[ESTWorkbenchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTWorkbenchCell"];
    }
    cell.cellType = section;
    return cell;
}
- (void)didClickRefreshClueButton:(TYZButton *)button {
    if (self.didClickRefreshButton) {
        self.didClickRefreshButton(button);
    }
}
- (void)didClickGetClueButton:(TYZButton *)button {
    if (self.didClickGetClueButton) {
        self.didClickGetClueButton(0);
    }
}
- (void)didClickChildButton:(TYZButton *)button {
    if (self.didSelectedChildBlock) {
        self.didSelectedChildBlock(button.tag - TAG_FIRST_CHILDVIEWBUTTONTags);
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    if (self.cellType == 0) {
        TYZImageView *imageView = [[TYZImageView alloc] initWithImage:[UIImage imageNamed:@"home_top_bg"]];
        [self.contentView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    
    CGFloat height = 0;
    if (self.cellType < 3) {
        height = 40;
        ESTSmallTitleView *titleView = [[ESTSmallTitleView alloc] init];
        [self.contentView addSubview:titleView];
        CGFloat padding = 10;
        if (self.cellType == 0) {
            padding = 20;
        }
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(padding);
            make.left.equalTo(self.contentView).offset(20);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(120);
        }];
        titleView.title = self.title;
        
        if (self.cellType == 0) {
            titleView.lineColor = TYZWhiteColor;
            TYZCustomSelectedView *selectedView = [[TYZCustomSelectedView alloc] init];
            [self.contentView addSubview:selectedView];
            [selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(titleView);
                make.right.equalTo(self.contentView).offset(-20);
                make.height.mas_equalTo(30);
                make.width.mas_equalTo(80);
            }];
            selectedView.selectedNumber = self.selectedNumber;
            selectedView.titleArray = @[[@"今日" S], [@"本月" S]];
            WEAKSELF
            [selectedView setDidSelectedBlock:^(NSInteger integer) {
                if (weakSelf.didSelectedDateTypeBlock) {
                    weakSelf.didSelectedDateTypeBlock(integer);
                }
            }];
        } else if (self.cellType == 1) {
            TYZButton *refreshButton = [[TYZButton alloc] init];
            [self.contentView addSubview:refreshButton];
            [refreshButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(titleView);
                make.centerX.equalTo(self.contentView.mas_right).offset(-50);
            }];
            [refreshButton addTarget:self action:@selector(didClickRefreshClueButton:) forControlEvents:UIControlEventTouchUpInside];
            [refreshButton setTitle:[@"刷新" S] forState:UIControlStateNormal];
            refreshButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
            [refreshButton setTitleColor:ThemeColor forState:UIControlStateNormal];
        }
    }
    
    TYZView *menuView = [[TYZView alloc] init];
    [self.contentView addSubview:menuView];
    [menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(height);
    }];
    
    if (self.cellType == 1) {
        TYZButton *getClueButton = [[TYZButton alloc] init];
        [menuView addSubview:getClueButton];
        [getClueButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(menuView.mas_right).offset(-50);
            make.top.equalTo(menuView).offset(10);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(30);
        }];
        [getClueButton setTitle:[@"抢" S] forState:UIControlStateNormal];
        getClueButton.layer.cornerRadius = 15;
        getClueButton.layer.masksToBounds = YES;
        if (self.clueArray.count > 0) {
            [getClueButton setBackgroundColor:ThemeColor];
            [getClueButton setTitleColor:TYZWhiteColor forState:UIControlStateNormal];
        } else {
            [getClueButton setBackgroundColor:TYZLightMMColor];
            [getClueButton setTitleColor:TYZWhiteColor forState:UIControlStateNormal];
        }
        [getClueButton addTarget:self action:@selector(didClickGetClueButton:) forControlEvents:UIControlEventTouchUpInside];
        [getClueButton setTitle:[@"抢" S] forState:UIControlStateNormal];
        getClueButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
        
        TYZLabel *clueLabel = [[TYZLabel alloc] init];
        [self.contentView addSubview:clueLabel];
        [clueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(20);
            make.centerY.equalTo(getClueButton);
        }];
        clueLabel.font = TYZFont(CoreInfo.fontSize * 0.8);
        if (self.clueArray.count > 0) {
            
        } else {
            clueLabel.textColor = TYZLightMColor;
            clueLabel.text = [@"暂时还没有可抢的公共线索呢" S];
        }
    } else {
        NSInteger count = 3;
        CGFloat height = ESTWorkbenchCellChildHeight1;
        CGFloat padding = ESTWorkbenchCellPadding;
        ESTVerticalValueViewBackgroundColor mode = BackgroundColor_White;
        if (self.cellType == 2) {
            count = 4;
            height = ESTWorkbenchCellChildHeight2;
            mode = BackgroundColor_Gray;
            padding = 0;
        }
        CGFloat width = kWindowWidth / count;
        if (self.cellType == 0 || self.cellType == 2) {
            [self.nameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ESTVerticalValueView *childView = [ESTVerticalValueView valueViewWithName:obj value:self.valueArray[idx] backgroundColorModel:mode];
                [menuView addSubview:childView];
                [childView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(menuView).offset(idx % count * width);
                    make.top.equalTo(menuView).offset(idx / count * height + padding);
                    make.height.mas_equalTo(height);
                    make.width.mas_equalTo(width);
                }];
                WEAKSELF
                [childView setDidClickCoverBlock:^{
                    if (weakSelf.didSelectedChildBlock) {
                        weakSelf.didSelectedChildBlock(idx);
                    }
                }];
            }];
        } else {
            [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TYZView *childView = [[TYZView alloc] init];
                [menuView addSubview:childView];
                [childView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(menuView).offset(idx % count * width);
                    make.top.equalTo(menuView).offset(idx / count * height);
                    make.height.mas_equalTo(height);
                    make.width.mas_equalTo(width);
                }];
                TYZImageView *imageView = [[TYZImageView alloc] initWithImage:[UIImage imageNamed:obj.firstObject]];
                [childView addSubview:imageView];
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(childView);
                    make.bottom.equalTo(childView.mas_centerY).offset(10);
                }];
                TYZLabel *label = [TYZLabel labelWithText:obj.lastObject];
                [childView addSubview:label];
                [label mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(childView);
                    make.top.equalTo(imageView.mas_bottom).offset(10);
                }];
                TYZButton *coverButton = [[TYZButton alloc] init];
                [childView addSubview:coverButton];
                [coverButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(childView);
                }];
                coverButton.tag = TAG_FIRST_CHILDVIEWBUTTONTags + idx;
                [coverButton addTarget:self action:@selector(didClickChildButton:) forControlEvents:UIControlEventTouchUpInside];
            }];
        }
    }
}
@end
